import pandas as pd
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col

# read in file 'table_4_data.csv' which should be in the same directory as this code
tb4 = pd.read_csv('table_4_pseudo_data.csv')

# run regressions from table 4
dfs = tbl4
reg1 = smf.ols(formula="rating_num ~  rating_second + moodys ", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})
reg2 = smf.ols(formula="rating_num ~  rating_second + moodys + C(year)", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})
reg3 = smf.ols(formula="rating_num ~  rating_second + moodys + C(year) + C(issuer_name)", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})
reg4 = smf.ols(formula="rating_num ~  rating_second*week_diff + moodys ", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})
reg5 = smf.ols(formula="rating_num ~  rating_second*week_diff + moodys + C(year) ", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})
reg6 = smf.ols(formula="rating_num ~  rating_second*week_diff + moodys + C(year) + C(issuer_name)", data=dfs).fit(cov_type='cluster', cov_kwds={'groups': dfs[['issuer_name']]})

# create table
results = summary_col([reg1, reg2, reg3, reg4, reg5, reg6],stars=True,float_format='%0.3f',
                  model_names=['1','2','3','4','5','6'],
                  info_dict={'N':lambda x: "{0:d}".format(int(x.nobs)),
                             'R2':lambda x: "{:.2f}".format(x.rsquared)},
                             regressor_order=[  'rating_second',
                                                'week_diff',
                                                'rating_second:week_diff'],
                             drop_omitted=True)
# print table
print(results)
